home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / local / sbin / unsafe-browser < prev   
Encoding:
Text File  |  2013-01-09  |  9.1 KB  |  264 lines

  1. #!/bin/sh
  2.  
  3. set -e
  4.  
  5. CMD=$(basename ${0})
  6. LOCK=/var/lock/${CMD}
  7.  
  8. . gettext.sh
  9. TEXTDOMAIN="${CMD}"
  10. export TEXTDOMAIN
  11.  
  12. ROFS=/live/rofs/filesystem.squashfs
  13. CONF_DIR=/var/lib/unsafe-browser
  14. COW=${CONF_DIR}/cow
  15. CHROOT=${CONF_DIR}/chroot
  16. CLEARNET_USER=clearnet
  17. OFFENDING_ADDONS="xul-ext-foxyproxy-standard xul-ext-torbutton xul-ext-firegpg
  18. xul-ext-cookie-monster xul-ext-noscript xul-ext-monkeysphere"
  19.  
  20. # Import tor_is_working()
  21. . /usr/local/lib/tails-shell-library/tor.sh
  22.  
  23. WARNING_PAGE='/usr/share/doc/tails/website/misc/unsafe_browser_warning'
  24. LANG_CODE="$(echo ${LANG} | head -c 2)"
  25. if [ -r "${WARNING_PAGE}.${LANG_CODE}.html" ]; then
  26.    START_PAGE="${WARNING_PAGE}.${LANG_CODE}.html"
  27. else
  28.    START_PAGE="${WARNING_PAGE}.en.html"
  29. fi
  30.  
  31. if [ -e /var/lib/gdm3/tails.camouflage ]; then
  32.     CAMOUFLAGE=yes
  33. fi
  34.  
  35. cleanup () {
  36.     # Break down the chroot and kill all of its processes
  37.     local counter=0
  38.     local ret=0
  39.     while [ "${counter}" -le 10 ] && \
  40.         pgrep -u ${CLEARNET_USER} 1>/dev/null 2>&1; do
  41.         pkill -u ${CLEARNET_USER} 1>/dev/null 2>&1
  42.         ret=${?}
  43.         sleep 1
  44.         counter=$((${counter}+1))
  45.     done
  46.     [ ${ret} -eq 0 ] || pkill -9 -u ${CLEARNET_USER} 1>/dev/null 2>&1
  47.     for mnt in ${CHROOT}/dev ${CHROOT}/proc ${CHROOT} ${COW}; do
  48.         counter=0
  49.         while [ "${counter}" -le 10 ] && mountpoint -q ${mnt} 2>/dev/null; do
  50.             umount ${mnt} 2>/dev/null
  51.             sleep 1
  52.             counter=$((${counter}+1))
  53.         done
  54.     done
  55.     rmdir ${COW} ${CHROOT} 2>/dev/null
  56. }
  57.  
  58. error () {
  59.     local cli_text="${CMD}: `gettext \"error:\"` ${@}"
  60.     local dialog_text="<b><big>`gettext \"Error\"`</big></b>
  61.  
  62. ${@}"
  63.     echo "${cli_text}" >&2
  64.     sudo -u ${SUDO_USER} zenity --error --title "" --text "${dialog_text}"
  65.     exit 1
  66. }
  67.  
  68. warning () {
  69.     local cli_text="${CMD}: `gettext \"warning:\"` ${text}"
  70.     local dialog_text="<b><big>`gettext \"Warning\"`</big></b>
  71.  
  72. ${@}"
  73.     echo "${cli_text}" >&2
  74.     sudo -u ${SUDO_USER} zenity --warning --title "" --text "${dialob_text}"
  75. }
  76.  
  77. verify_start () {
  78.     # Make sure the user really wants to start the browser
  79.     local dialog_msg="<b><big>`gettext \"Do you really want to launch the Unsafe Browser?\"`</big></b>
  80.  
  81. `gettext \"Network activity within the Unsafe Browser is <b>not anonymous</b>. Only use the Unsafe Browser if necessary, for example if you have to login or register to activate your Internet connection.\"`"
  82.     local launch="`gettext \"_Launch\"`"
  83.     local exit="`gettext \"_Exit\"`"
  84.     # Since zenity can't set the default button to cancel, we switch the
  85.     # labels and interpret the return value as its negation.
  86.     if sudo -u ${SUDO_USER} zenity --question --title "" --ok-label "${exit}" \
  87.        --cancel-label "${launch}" --text "${dialog_msg}"; then
  88.         exit 0
  89.     fi
  90. }
  91.  
  92. show_start_notification () {
  93.     local title="`gettext \"Starting the Unsafe Browser...\"`"
  94.     local body="`gettext \"This may take a while, so please be patient.\"`"
  95.     notify-send -t 10000 "${title}" "${body}"
  96. }
  97.  
  98. setup_chroot () {
  99.     # Setup a chroot on an aufs "fork" of the filesystem.
  100.     # FIXME: When LXC matures to the point where it becomes a viable option
  101.     # for creating isolated jails, the chroot can be used as its rootfs.
  102.     echo "* Setting up chroot"
  103.  
  104.     trap cleanup INT
  105.     trap cleanup EXIT
  106.  
  107.     mkdir -p ${COW} ${CHROOT} && \
  108.     mount -t tmpfs tmpfs ${COW} && \
  109.     mount -t aufs -o noatime,noxino,dirs=${COW}=rw:${ROFS}=rr+wh aufs ${CHROOT} && \
  110.     mount -t proc proc ${CHROOT}/proc && \
  111.     mount --bind /dev ${CHROOT}/dev || \
  112.     error "`gettext \"Failed to setup chroot.\"`"
  113. }
  114.  
  115. set_chroot_browser_name () {
  116.     NAME="${1}"
  117.     LONG=$(echo ${LANG} | grep -o "^[a-zA-Z_]*")
  118.     SHORT=${LONG%%_*}
  119.     EXT_DIR=${CHROOT}/usr/lib/iceweasel/extensions
  120.     BRANDING=branding/brand.dtd
  121.     if [ -e "${EXT_DIR}/langpack-${LONG}@iceweasel.mozilla.org.xpi" ]; then
  122.         PACK="${EXT_DIR}/langpack-${LONG}@iceweasel.mozilla.org.xpi"
  123.         TOP=chrome
  124.         REST=${LONG}/locale
  125.     elif [ -e "${EXT_DIR}/langpack-${SHORT}@iceweasel.mozilla.org.xpi" ]; then
  126.         PACK="${EXT_DIR}/langpack-${SHORT}@iceweasel.mozilla.org.xpi"
  127.         TOP=chrome
  128.         REST=${SHORT}/locale
  129.     else
  130.         PACK=${CHROOT}/usr/share/iceweasel/chrome/en-US.jar
  131.         TOP=locale
  132.         REST=
  133.     fi
  134.  
  135.     TMP=$(mktemp -d)
  136.     7z x "${PACK}" -o"${TMP}"
  137.     sed -i "s/Iceweasel/${NAME}/" "${TMP}"/"${TOP}"/"${REST}"/"${BRANDING}"
  138.     7z u -tzip "${PACK}" "${TMP}"/"${TOP}"
  139.     chmod a+r "${PACK}"
  140.     rm -Rf "${TMP}"
  141. }
  142.  
  143. configure_chroot () {
  144.     echo "* Configuring chroot"
  145.  
  146.     # Set the chroot's DNS servers to those obtained through DHCP
  147.     rm -f ${CHROOT}/etc/resolv.conf
  148.     for NS in ${IP4_NAMESERVERS}; do
  149.         echo "nameserver ${NS}" >> ${CHROOT}/etc/resolv.conf
  150.     done
  151.     chmod a+r ${CHROOT}/etc/resolv.conf
  152.  
  153.     # Remove all Iceweasel addons: some adds proxying, which we don't
  154.     # want; some may change the fingerprint compared to a standard
  155.     # Iceweasel install.
  156.     chroot ${CHROOT} apt-get remove --yes xul-ext-*
  157.  
  158.     # Disable proxying in the chroot
  159.     sed -i '/^pref("network.proxy.type",/d' \
  160.         ${CHROOT}/etc/iceweasel/pref/iceweasel.js
  161.     echo 'pref("network.proxy.type", 0);' >> \
  162.         ${CHROOT}/etc/iceweasel/pref/iceweasel.js
  163.     rm -rf ${CHROOT}/etc/iceweasel/profile/extensions
  164.  
  165.     # Set a scary theme (except if we're using Windows camouflage)
  166.     if [ -z "${CAMOUFLAGE}" ]; then
  167.         cat >> ${CHROOT}/etc/iceweasel/profile/user.js <<EOF
  168. user_pref("lightweightThemes.isThemeSelected", true);
  169. user_pref("lightweightThemes.usedThemes", "[{\"id\":\"1\",\"name\":\"Unsafe Browser\",\"headerURL\":\"file:///usr/share/pixmaps/red_dot.png\",\"footerURL\":\"file:///usr/share/pixmaps/red_dot.png\",\"textcolor\":\"#FFFFFF\",\"accentcolor\":\"#CC0000\",\"updateDate\":0,\"installDate\":0}]");
  170. EOF
  171.     else
  172.         echo "LIVE_USERNAME=${CLEARNET_USER}" > \
  173.             ${CHROOT}//etc/live/config.d/username.conf
  174.         chroot ${CHROOT} tails-activate-winxp-theme
  175.     fi
  176.  
  177.     # Set the name (e.g. window title) of the browser
  178.     set_chroot_browser_name "`gettext \"Unsafe Browser\"`"
  179.  
  180.     # Set start page to something that explains what's going on
  181.     echo 'user_pref("browser.startup.homepage", "'${START_PAGE}'");' >> \
  182.         ${CHROOT}/etc/iceweasel/profile/user.js
  183.  
  184.     # Remove all bookmarks
  185.     rm -f ${CHROOT}/etc/iceweasel/profile/bookmarks.html
  186.  
  187.     # Make the English wikipedia the only available and default search
  188.     # engine (this is a documented cue for identifying the Unsafe Browser,
  189.     # which is extra important in camouflage mode when the scary coloured
  190.     # theme is disabled.)
  191.     find ${CHROOT}/etc/iceweasel/profile/searchplugins/ \
  192.          ${CHROOT}/etc/iceweasel/searchplugins -name "*.xml" | \
  193.         while read searchengine; do
  194.             if basename "$searchengine" | grep -qvi wikipedia; then
  195.                 rm "${searchengine}"
  196.             fi
  197.         done
  198.     sed -i '/^user_pref("browser.search.defaultenginename",/d' \
  199.         ${CHROOT}/etc/iceweasel/profile/user.js
  200.     echo 'user_pref("browser.search.defaultenginename", "Wikipedia (en)");' >> \
  201.         ${CHROOT}/etc/iceweasel/profile/user.js
  202.     sed -i '/^user_pref("browser.search.selectedEngine",/d' \
  203.         ${CHROOT}/etc/iceweasel/profile/user.js
  204.     echo 'user_pref("browser.search.selectedEngine", "Wikipedia (en)");' >> \
  205.         ${CHROOT}/etc/iceweasel/profile/user.js
  206. }
  207.  
  208. run_browser_in_chroot () {
  209.     # Start Iceweasel in the chroot
  210.     echo "* Starting Unsafe Browser"
  211.  
  212.     sudo -u ${SUDO_USER} xhost +SI:localuser:${CLEARNET_USER} 2>/dev/null
  213.     chroot ${CHROOT} sudo -u ${CLEARNET_USER} iceweasel -DISPLAY=:0.0
  214.     sudo -u ${SUDO_USER} xhost -SI:localuser:${CLEARNET_USER} 2>/dev/null
  215. }
  216.  
  217. show_shutdown_notification () {
  218.     local title="`gettext \"Shutting down the Unsafe Browser...\"`"
  219.     local body="`gettext \"This may take a while, and you may not restart the Unsafe Browser until it is properly shut down.\"`"
  220.     notify-send -t 10000 "${title}" "${body}"
  221. }
  222.  
  223. maybe_restart_tor () {
  224.     # Restart Tor if it's not working (a captive portal may have prevented
  225.     # Tor from bootstrapping, and a restart is the fastest way to get
  226.     # wheels turning)
  227.     if ! tor_is_working; then
  228.         echo "* Restarting Tor"
  229.         restart-tor
  230.         if ! service tor status >/dev/null; then
  231.             error "`gettext \"Failed to restart Tor.\"`"
  232.         fi
  233.     fi
  234. }
  235.  
  236. # Prevent multiple instances of the script.
  237. exec 9>${LOCK}
  238. if ! flock -x -n 9; then
  239.     error "`gettext \"Another Unsafe Browser is currently running, or being cleaned up. Please retry in a while.\"`"
  240. fi
  241.  
  242. # Get the DNS servers that was obtained from NetworkManager, if any...
  243. NM_ENV=/var/lib/NetworkManager/env
  244. if [ -r "${NM_ENV}" ]; then
  245.     . ${NM_ENV}
  246. fi
  247. # ... otherwise fail.
  248. # FIXME: Or would it make sense to fallback to Google's DNS or OpenDNS?
  249. # Some stupid captive portals may allow DNS to any host, but chances are
  250. # that only the portal's DNS would forward to the login page.
  251. if [ -z "${IP4_NAMESERVERS}" ]; then
  252.     error "`gettext \"No DNS server was obtained through DHCP or manually configured in NetworkManager.\"`"
  253. fi
  254.  
  255. verify_start
  256. show_start_notification
  257. setup_chroot
  258. configure_chroot
  259. run_browser_in_chroot
  260. show_shutdown_notification
  261. maybe_restart_tor
  262.  
  263. exit 0
  264.